package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyWildCameraAttachQuery;
import com.zl.tby.model.TbyWildCameraAttach;
import com.zl.tby.repository.TbyWildCameraAttachRepo;
import com.zl.tby.service.ITbyWildCameraAttachService;
import org.noear.solon.annotation.Component;
import org.noear.wood.IPage;
import org.noear.wood.annotation.Db;
import org.noear.wood.utils.StringUtils;

import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 相机信息
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbyWildCameraAttachServiceImpl implements ITbyWildCameraAttachService {

    @Db
    private TbyWildCameraAttachRepo tbyWildCameraAttachRepo;

    @Override
    public TbyWildCameraAttach get(Long id) {
        return tbyWildCameraAttachRepo.selectById(id);
    }

    @Override
    public R<Void> delete(Long id) {
        int flag = tbyWildCameraAttachRepo.deleteById(id);
        return flag > 0 ? R.ok() : R.failed("删除失败");
    }

    @Override
    public TbyPageResult<TbyWildCameraAttach> page(TbyWildCameraAttachQuery query) {
        IPage<TbyWildCameraAttach> iPage = tbyWildCameraAttachRepo.selectPage(query.getOffset(), query.getSize(), mp -> {
            mp.whereTrue()
                    .andIf(StringUtils.isNotEmpty(query.getSpeciesCode()), "species_code = ?", query.getSpeciesCode())
                    .andIf(StringUtils.isNotEmpty(query.getSpeciesName()), "species_name like ?", "%" + query.getSpeciesName() + "%")
                    .andIf(StringUtils.isNotEmpty(query.getFileType()), "file_type = ?", query.getFileType())
                    .andIf(StringUtils.isNotEmpty(query.getShotDateBegin()), "shot_date >= ?", query.getShotDateBegin() + " 00:00:00")
                    .andIf(StringUtils.isNotEmpty(query.getShotDateEnd()), "shot_date <= ?", query.getShotDateEnd() + " 23:59:59")
                    .andIf(query.getCameraId() != null, "camera_id = ?", query.getCameraId());
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mp.orderBy(query.getOrder());
            }
        });
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        iPage.getList().forEach(item -> {
            if (item.getShotDate() != null) {
                item.setShotDateFormat(item.getShotDate().format(formatter));
            }
        });
        return new TbyPageResult(iPage, query);
    }

    @Override
    public List<TbyWildCameraAttach> list(TbyWildCameraAttachQuery query) {
        List<TbyWildCameraAttach> dataList = tbyWildCameraAttachRepo.selectList(mapperWhereQ -> {
            mapperWhereQ.whereTrue()
                    .andIf(StringUtils.isNotEmpty(query.getSpeciesCode()), "species_code = ?", query.getSpeciesCode())
                    .andIf(StringUtils.isNotEmpty(query.getSpeciesName()), "species_name like ?", "%" + query.getSpeciesName() + "%")
                    .andIf(StringUtils.isNotEmpty(query.getFileType()), "file_type = ?", query.getFileType())
                    .andIf(StringUtils.isNotEmpty(query.getShotDateBegin()), "shot_date >= ?", query.getShotDateBegin())
                    .andIf(StringUtils.isNotEmpty(query.getShotDateEnd()), "shot_date <= ?", query.getShotDateEnd())
                    .andIf(query.getCameraId() != null, "camera_id = ?", query.getCameraId());
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mapperWhereQ.orderBy(query.getOrder());
            }
        });
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        dataList.forEach(item -> {
            if (item.getShotDate() != null) {
                item.setShotDateFormat(item.getShotDate().format(formatter));
            }
        });
        return dataList;
    }

    @Override
    public R<Void> updateSpecies(Long id, Long speciesId, String speciesCode, String speciesName) {
        TbyWildCameraAttach entity = new TbyWildCameraAttach();
        entity.setId(id);
        entity.setSpeciesId(speciesId);
        entity.setSpeciesCode(speciesCode);
        entity.setSpeciesName(speciesName);
        this.tbyWildCameraAttachRepo.updateById(entity, true);
        return R.ok();
    }
}
